package com.example.sihookuiklydemo.base

import com.tencent.kuikly.core.base.Animation
import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.Translate
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.layout.undefined
import com.tencent.kuikly.core.layout.valueEquals
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.views.ActivityIndicator
import com.tencent.kuikly.core.views.Image
import com.tencent.kuikly.core.views.SwitchView
import com.tencent.kuikly.core.views.View

class ProgressDialogView : ComposeView<ProgressDialogAttr,ProgressDialogEvent>() {
    private var isOn by observable(false)
    private val width : Float
        get() = flexNode.styleWidth
    private val height : Float
        get() = flexNode.styleHeight

    var animated: Boolean by observable(false)
    override fun created() {
        super.created()

    }
    override fun attr(init:ProgressDialogAttr.() -> Unit) {

        super.attr(init)
    }

    override fun body(): ViewBuilder {

        val ctx = this
        return {
            attr {

                absolutePosition(0f, 0f, 0f, 0f)
                justifyContentCenter()
                alignItemsCenter()
                //backgroundColor(Color(0, 0, 0, 0.5f))
               /* if (ctx.animated) {
                    backgroundColor(Color(0, 0, 0, 0.5f))
                } else {
                    backgroundColor(Color(0, 0, 0, 0f))
                }
                animation(Animation.springEaseIn(0.5f, 0.92f, 1f), ctx.animated)*/
            }

            View {
                attr {
                    allCenter()
                    size(100f,100f)
                    marginLeft(12f)
                    marginRight(12f)
                    backgroundColor(0xff212121)
                    borderRadius(16f)
                    /*if (ctx.animated) {
                        transform(Translate(0f, 0f))
                    } else {
                        transform(Translate(0f, 1f))
                    }
                    animation(Animation.linear(), ctx.animated)*/
                }
                ActivityIndicator {  }
            }



        }
    }

    //
    override fun createAttr(): ProgressDialogAttr =ProgressDialogAttr()

    override fun createEvent(): ProgressDialogEvent =ProgressDialogEvent()
    override fun viewDidLayout() {
        super.viewDidLayout()
        animated = true
    }
}

class ProgressDialogAttr : ComposeAttr() {

}

class ProgressDialogEvent : ComposeEvent() {
    internal var switchOnChangedHandlerFn : ((on : Boolean) -> Unit)? = null
    // 开关变化时回调
    fun switchOnChanged(handlerFn: (on : Boolean) -> Unit) {
        switchOnChangedHandlerFn = handlerFn
    }

}


fun ViewContainer<*, *>.ProgressDialog(init: ProgressDialogView.() -> Unit) {
    addChild(ProgressDialogView(), init)
}